package definite.Guide.Book.BoundedRangeComponents;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.util.Hashtable;

import javax.swing.BoundedRangeModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import definite.Guide.Book.ToggleButton.DiamondIcon;

public class JSliderSample {

	double d = 0;

	static class BoundChangedListener implements ChangeListener {

		@Override
		public void stateChanged(ChangeEvent e) {
			Object source = e.getSource();
			if (source instanceof BoundedRangeModel) {
				BoundedRangeModel aModel = (BoundedRangeModel) source;
				/*if (!aModel.getValueIsAdjusting())
					System.out.println("Changed : " + aModel.getValue());
				else {
					if (source instanceof JSlider) {
						JSlider slider = (JSlider) source;
						if (!slider.getValueIsAdjusting())
							System.out.println("Slider changed : "
								+ slider.getValue());
					} else
						System.out.println("Something changed :" + source);
				}*/

				System.out.println("Changed : " + aModel.getValue());
			}
		}

	}

	public static void main(String[] args) {

		final Hashtable<Integer, JLabel> table =
			new Hashtable<Integer, JLabel>();
		table.put(0, new JLabel(new DiamondIcon(Color.RED)));
		table.put(10, new JLabel("Ten"));
		table.put(25, new JLabel("Twenty-Five"));
		table.put(34, new JLabel("Thirty-Four"));
		table.put(52, new JLabel("Fifty-Two"));
		table.put(70, new JLabel("Seventy"));
		table.put(82, new JLabel("Eighty-Two"));
		table.put(100, new JLabel(new DiamondIcon(Color.BLACK)));

		Runnable r = new Runnable() {

			@Override
			public void run() {
				JFrame.setDefaultLookAndFeelDecorated(true);
				JFrame frame = new JFrame("Slider Sampler");
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

				JSlider aSlider = new JSlider();
				aSlider.setLabelTable(table);
				aSlider.setPaintLabels(true);
				aSlider.setPaintTicks(true);
				aSlider.setMajorTickSpacing(10);
				// setInverted(true) -> true to reverse the slider values from
				// their normal order
				aSlider.setInverted(false);
				// setPaintTract(false) -> hide the paint track
				aSlider.setPaintTrack(true);
				// listen to JSlider event
				ChangeListener event = new BoundChangedListener();
				BoundedRangeModel model = aSlider.getModel();
				model.addChangeListener(event);

				frame.add(aSlider, BorderLayout.CENTER);
				frame.setLocationRelativeTo(null);
				frame.setSize(300, 200);
				frame.setVisible(true);
			}
		};

		EventQueue.invokeLater(r);
	}

}
